<?php

/**
 * This is the model class for table "comanda".
 *
 * The followings are the available columns in table 'comanda':
 * @property integer $id
 * @property string $data
 * @property string $hora
 * @property integer $persona_id
 * @property integer $taula_id
 * @property integer $estat_id
 *
 * The followings are the available model relations:
 * @property Persona $persona
 * @property Taula $taula
 * @property Estat $estat
 * @property Comandalin[] $comandalins
 */
class Comanda extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comanda the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'comanda';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('data, hora, persona_id, taula_id, estat_id', 'required'),
			array('persona_id, taula_id, estat_id', 'numerical', 'integerOnly'=>true),
            array('data', 'type', 'type'=>'date', 'dateFormat'=>Yii::app()->locale->dateFormat),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, data, hora, persona_id, taula_id, estat_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comandaPersona' => array(self::BELONGS_TO, 'Persona', 'persona_id'),
			'comandaTaula' => array(self::BELONGS_TO, 'Taula', 'taula_id'),
			'comandaEstat' => array(self::BELONGS_TO, 'Estat', 'estat_id'),
			'comandaLines' => array(self::HAS_MANY, 'Comandalin', 'comanda_id'),
            'comandaNumLines' => array(self::STAT, 'Comandalin', 'comanda_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Número',
			'data' => 'Data',
			'hora' => 'Hora',
			'persona_id' => 'Responsable',
			'taula_id' => 'Taula',
			'estat_id' => 'Estat',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('data',$this->data,true);
		$criteria->compare('hora',$this->hora,true);
		$criteria->compare('persona_id',$this->persona_id);
		$criteria->compare('taula_id',$this->taula_id);
		$criteria->compare('estat_id',$this->estat_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>array(
                    'data'=>false, //false= ordre ascendent, true= ordre descendent
                    'hora'=>false,
                )),
            'pagination'=>array(
                'pageSize'=>Yii::app()->params['itemsPagina']),
		));
	}
    
    protected function beforeSave()
    {   
        foreach($this->metadata->tableSchema->columns as $columnName => $column){
                if ($column->dbType == 'date'){
                        $this->$columnName = date('Y-m-d', CDateTimeParser::parse($this->$columnName, Yii::app()->locale->dateFormat));
                }elseif ($column->dbType == 'datetime'){
                        $this->$columnName = date('Y-m-d H:i:s', CDateTimeParser::parse($this->$columnName, Yii::app()->locale->dateFormat));
                }
        
        }
        
        return true;
    }
    
    protected function afterFind()
    {
       foreach($this->metadata->tableSchema->columns as $columnName => $column){
                        
                if (!strlen($this->$columnName)) continue;
                        
                if ($column->dbType == 'date'){                         
                        $this->$columnName = Yii::app()->dateFormatter->formatDateTime(
                                        CDateTimeParser::parse($this->$columnName, 'yyyy-MM-dd'),'medium',null);
                        }elseif ($column->dbType == 'datetime'){
                                $this->$columnName = Yii::app()->dateFormatter->formatDateTime(
                                        CDateTimeParser::parse($this->$columnName, 'yyyy-MM-dd hh:mm:ss'));
                        }
        }
        return true;
    }
    
    public function afegirLinia($linia) {
        $linia->comanda_id= $this->id;
        return $linia->save();
    }
}